//
//  SCCheckBigPhotoView.m
//  Shuaiche
//
//  Created by dangshuai on 2019/4/26.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCCheckBigPhotoView.h"

@interface SCCheckBigPhotoView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UIButton *backButton;
@end

@implementation SCCheckBigPhotoView


+ (instancetype)showViewImage:(UIImage *)image {
    if (!image) {return nil;}
    SCCheckBigPhotoView *view = [[SCCheckBigPhotoView alloc] init];
    view.showImage = image;
    if (image.size.height > image.size.width) {
        view.scrollView.hidden = NO;
    } else {
        [view addSubview:view.imageView];
        view.imageView.center = CGPointMake(kSCScreenWidth/2.f, kSCScreenHeight/2.f);
    }
    [view showView];
    return view;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = SC_RGB(34, 34, 34);
        self.frame = CGRectMake(0, 0, kSCScreenWidth, kSCScreenHeight);
    }
    return self;
}

- (void)deleteButtonClick {
    self.closeBlock(YES);
    [self hideView];
}

- (void)backButtonclick {
    [self hideView];
}

- (void)showView {
    self.alpha = 0;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 1;
    }];
}

- (void)hideView {
    [UIView animateWithDuration:.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, kSCScreenHeight)];
        [self addSubview:_scrollView];
        
        CGFloat h = _showImage.size.height/_showImage.size.width * kSCScreenWidth;
        [_scrollView addSubview:self.imageView];
        _imageView.frame = CGRectMake(0, 0, kSCScreenWidth, h);
        _scrollView.contentSize = CGSizeMake(kSCScreenWidth, h);
        
        UIView *nav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, kSCNavigationHeight)];
        nav.backgroundColor = SC_RGBA(0, 0, 0, 0.5);
        [self addSubview:nav];
        [nav addSubview:self.deleteButton];
        [nav addSubview:self.backButton];
    }
    return _scrollView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:_showImage];
        _imageView.bounds = CGRectMake(0, 0, kSCScreenWidth, kSCScreenWidth * _showImage.size.height/_showImage.size.width);
        [self addSubview:self.deleteButton];
        [self addSubview:self.backButton];
    }
    return _imageView;
}

- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.frame = CGRectMake(kSCScreenWidth - 60, kSCStatusBarHeight, 60, 44);
        _deleteButton.titleLabel.font = SC_FONT(15);
        [_deleteButton setTitle:@"删除" forState:0];
        [_deleteButton setTitleColor:[UIColor whiteColor] forState:0];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.frame = CGRectMake(0, kSCStatusBarHeight, 60, 44);
        _backButton.titleLabel.font = SC_FONT(15);
        [_backButton setTitle:@"确定" forState:0];
//        [_backButton setImage:[UIImage imageNamed:@"icon_white_back"] forState:0];
        [_backButton setTitleColor:[UIColor whiteColor] forState:0];
        [_backButton addTarget:self action:@selector(backButtonclick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

@end
